import 'dart:ui';

import 'package:flutter/material.dart';

String _assetImagePrefix = "assets/images/";

//TODO:wrapAsset
/// 资源类扩展方法
extension HkWrapAsset on String {
  /// 图片url
  String get assetImg => _assetImagePrefix + this;

  /// 选中图片的url
  /// 必须满足xxx_selected.png
  String get assetImgSelected {
    var split = this.split(".");
    return "${_assetImagePrefix + split[0]}_selected.${split[1]}";
  }
}

//TODO:wrapColor
extension HkWrapColor on String {
  Color get hkColor {
    var hexColor = toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return Color(int.parse(hexColor, radix: 16));
  }
}

//TODO:wrapString
extension HkWrapString on String {

  // 是否是在线url
  bool isOnlineImageUrl() {
    Uri? uri = Uri.tryParse(this);
    if (uri != null && (uri.scheme == 'http' || uri.scheme == 'https')) {
      return true;
    }
    return false;
  }
  /// 修复换行符无法解析
  String get fixLineBreak {
    return replaceAll("\\n", "\n");
  }

  /// 移除所有空格
  String get removeAllTrim {
    return replaceAll(RegExp(r"\s+\b|\b\s"), "");
  }

  /// 移除标点符号
  String get removePunctuation {
    String msg = "";
    if (contains("。")) {
      msg = replaceAll("。", "");
    }
    if (contains("，")) {
      msg = replaceAll("，", "");
    }
    if (contains("？")) {
      msg = replaceAll("？", "");
    }
    return msg.isNotEmpty ? msg : this;
  }

  /// 字符串是否为空
  bool get empty {
    return this == "";
  }

  bool isNotEmpty() {
    return this.isNotEmpty;
  }

  /// 字符串替换：去头, 去尾 中间 *
  String safeReplaceAll() {
    return replaceRange(1, length - 1, '*' * (length - 2));
  }

  /// 字符串替换：去头, 去尾 中间 用其他字符替换
  /// [replaceString] 用来替换的字符串
  /// [replaceStringCount] 替换字符串的长度, 默认为text.length - 2
  String replaceWith({String replaceString = '*', int? replaceStringCount}) {
    if (empty) {
      return this;
    }
    return replaceRange(
        1, length - 1, replaceString * (replaceStringCount ?? (length - 2)));
  }

  /// 手机号加密显示
  String get safeMdn {
    if (length < 11) {
      return "手机号格式不正确";
    }
    const int startLength = 3;
    const int replaceLength = 4;
    const String replacement = "****";
    return replaceRange(startLength, startLength + replaceLength, replacement);
  }

  /// 正则校验-手机号
  bool get isMdn {
    if (empty || length < 11) return false;
    //手机正则验证
    String regexPhoneNumber =
        r"^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$";
    return RegExp(regexPhoneNumber).hasMatch(this);
  }

  /// 添加前置0
  String addLeadingZero({int length = 2}) {
    return (this).padLeft(length, '0');
  }

  ///邮箱验证
  static bool isEmail(String str) {
    return RegExp(r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
        .hasMatch(str);
  }

  ///验证URL
  static bool isUrl(String value) {
    return RegExp(r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+").hasMatch(value);
  }

  ///验证身份证
  static bool isIdCard(String value) {
    return RegExp(r"\d{17}[\d|x]|\d{15}").hasMatch(value);
  }

  ///验证中文
  static bool isChinese(String value) {
    return RegExp(r"[\u4e00-\u9fa5]").hasMatch(value);
  }
}

extension HkThemeExtension on BuildContext {
  /// 主题色
  Color get primaryColor => Theme.of(this).primaryColor;

  bool get isDark => Theme.of(this).brightness == Brightness.dark;

  Color get backgroundColor => Theme.of(this).scaffoldBackgroundColor;

  Color get dialogBackgroundColor => Theme.of(this).canvasColor;
}